﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KhuyenMaiVang.Models;
using KhuyenMaiVang.DAL;
using KhuyenMaiVang.Helpers;

namespace KhuyenMaiVang.Controllers
{
    public class CartController : Controller
    {
        //
        // GET: /Cart/
        private KhuyenMaiVangDataContext db = new KhuyenMaiVangDataContext();
        public ActionResult Index()
        {
            List<int> ids = getSession();
            List<Voucher> lstVouchers = new List<Voucher>();
            if (ids != null)
            {
                foreach (int id in ids)
                {
                    Voucher vouch = db.Vouchers.Single(m => m.ID == id);
                    lstVouchers.Add(vouch);
                }
            }
            return View(lstVouchers);
        }
        //
        // GET: /Cart/Add/id

        public ActionResult Add(int id)
        {
            List<int> ids = getSession();
            if (ids == null)
            {
                ids = new List<int>();
            }
            ids.Add(id);
            Session["myVouchers"] = ids;
            return RedirectToAction("Index");
        }
        public ActionResult Remove(int id)
        {
            List<int> ids = getSession();
            if (ids != null)
            {
                ids.RemoveAt(id);
                Session["myVouchers"] = ids;
            }
            return RedirectToAction("Index");
        }
        public ActionResult RemoveAll()
        {
            List<int> ids = getSession();
            if (ids != null)
            {
                Session["myVouchers"] = null;
            }
            return RedirectToAction("Index", "Home");
        }
        /**
         * Helper
         * */
        public List<int> getSession()
        {
            List<int> ids = Session["myVouchers"] != null ? (List<int>)Session["myVouchers"] : null;
            return ids;
        }
    }
}

